package view;

import model.LibrarySystem;
import model.Resource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {
    private final LibrarySystem system;
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private SearchPanel searchPanel;
    private BorrowPanel borrowPanel;
    private ReturnPanel returnPanel;

    public MainFrame(LibrarySystem system) {
        this.system = system;
        setTitle("图书馆管理系统");
        setSize(900, 650);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        initUI();
    }

    private void initUI() {
        // 创建卡片布局的面板
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);

        // 创建各功能面板
        searchPanel = new SearchPanel(system, this);
        borrowPanel = new BorrowPanel(system, this);
        returnPanel = new ReturnPanel(system, this);

        // 添加卡片
        cardPanel.add(searchPanel, "search");
        cardPanel.add(borrowPanel, "borrow");
        cardPanel.add(returnPanel, "return");

        // 创建导航按钮
        JPanel navPanel = createNavigationPanel();

        // 菜单栏
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("文件");
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(e -> System.exit(0));
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        // 添加组件到主窗口
        add(navPanel, BorderLayout.NORTH);
        add(cardPanel, BorderLayout.CENTER);
    }

    private JPanel createNavigationPanel() {
        JPanel navPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        navPanel.setBackground(new Color(70, 130, 180));
        navPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建带图标的导航按钮
        JButton searchBtn = createNavButton("资源搜索", UIManager.getIcon("FileView.fileIcon"));
        JButton borrowBtn = createNavButton("借阅资源", UIManager.getIcon("OptionPane.informationIcon"));
        JButton returnBtn = createNavButton("归还资源", UIManager.getIcon("OptionPane.questionIcon"));

        // 添加按钮事件
        searchBtn.addActionListener(e -> cardLayout.show(cardPanel, "search"));
        borrowBtn.addActionListener(e -> {
            cardLayout.show(cardPanel, "borrow");
            borrowPanel.refresh();
        });
        returnBtn.addActionListener(e -> {
            cardLayout.show(cardPanel, "return");
            returnPanel.refresh();
        });

        navPanel.add(searchBtn);
        navPanel.add(borrowBtn);
        navPanel.add(returnBtn);

        return navPanel;
    }

    private JButton createNavButton(String text, Icon icon) {
        JButton button = new JButton(text, icon);
        button.setFont(new Font("微软雅黑", Font.BOLD, 16));
        button.setBackground(new Color(240, 248, 255));
        button.setForeground(new Color(25, 25, 112));
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(70, 130, 180), 2), // 添加边框厚度（例如 2）
                BorderFactory.createEmptyBorder(10, 20, 10, 20)
        ));
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        return button;
    }

    public void refreshAll() {
        searchPanel.refresh();
        borrowPanel.refresh();
        returnPanel.refresh();
    }

    public void showResourceDetails(Resource resource) {
        new ResourceDetailsDialog(this, resource).setVisible(true);
    }

    public void showPanel(String panelName) {
        cardLayout.show(cardPanel, panelName);
    }
}